<template>
  <div>
    <el-card style="width: 600px">
      <el-form :model="form" label-width="80px" size="small">
        <div style="text-align: center; margin: 10px 0">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:9001/order/files/upload/"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
          >
            <img v-if="form.avatarUrl" :src="form.avatarUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
        <el-form-item label="用户名称">
          <el-input style="width: 80%" v-model="form.name" />
        </el-form-item>
        <el-form-item label="用户性别">
          <el-radio v-model="form.sex" label="男" size="large">男</el-radio>
          <el-radio v-model="form.sex" label="女" size="large">女</el-radio>
        </el-form-item>
        <el-form-item label="用户账号">
          <el-input style="width: 80%" v-model="form.loginAct" />
        </el-form-item>
        <el-form-item label="用户密码">
          <el-input style="width: 80%" v-model="form.loginPwd" />
        </el-form-item>
        <el-form-item label="用户邮箱">
          <el-input style="width: 80%" v-model="form.email" />
        </el-form-item>
      </el-form>

      <div style="text-align: center">
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Person",
  data() {
    return {
      form: {}
    }
  },
  created() {
    this.updateUser();
  },
  methods: {
    save() {
      //点击保存
      request.put("/order/admin/save", this.form).then(res => {
        if (res.code === 200) {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.updateUser()
          //子动父也动
          this.$emit('refreshUser')
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    updateUser() {
      //更新一下用户最新数据
      let userJson = sessionStorage.getItem("user") || "{}"
      if(userJson){
        let user = JSON.parse(userJson)
        request.get("order/admin/getUserById",{params:{id:user.id}}).then((res) =>{
          this.form=res.data
        })
        this.form.status="0"
      }else{
        this.$message.error("当前无法修改用户信息!");
        return
      }
    },
    handleAvatarSuccess(res) {
      this.form.avatarUrl = res.data
      this.$message.success("上传成功")
      // this.update()
    },
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>